// pages/lt_new/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'',//标题
    content:'', //内容
    fileList:[], //文件列表
    
  },
  onContentChange:function(e){
    var that = this;
    console.log(e)
    var value = e.detail;
    that.setData({
      content:value
    })
   
  },
  onTitleChange:function(e){
    var that = this;
    console.log(e)
    var value = e.detail;
    that.setData({
      title:value
    })
  },
  afterRead:function(e){
    console.log(e)
    const that = this;
    var fileList= that.data.fileList;
    var file = e.detail.file;
    fileList.push(file);
    this.setData({
      fileList:fileList
    })
  },

  submit:function(){
    var that = this;
    var title = that.data.title;
    var content = that.data.content;
    var fileList = that.data.fileList;
    if(!title){
      wx.showToast({
        title: "标题不能为空！",
      })
      return;
    }
    if(!content){
      wx.showToast({
        title: "内容不能为空！",
      })
      return;
    }
    if(fileList.length == 0){
      wx.showToast({
        title: "至少选择一张图片！",
      })
      return;
    }

    var item = {
      title:title,
      content:content
    }
    wx.showLoading({
      title: '正在提交...',
    })
    var count = fileList.length;
    console.log(count)
    var lineList = [];
    for(let i = 0; i < fileList.length; i++){
      var cur = fileList[i]
      that.upload(cur.url).then(d => {
        console.log(d)
        let url = d.data;
        console.log(url)
        lineList.push({"url":url})
      }).then(()=>{
        console.log(count)
        if(i == 0){
          console.log("进入了if....")
          item.imgs = lineList;

          var list = wx.getStorageSync('ltList');
          if(!list){
            list = [item]
          }else{
            list.unshift(item)
          }

          console.log(list)
          wx.setStorageSync('ltList', list)
          wx.hideLoading();
          wx.showToast({
            title: '保存成功！',
          })
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1,
            })
          },1000)
        }
      })
    }
    


    
  },
  //上传
  upload:function(filePath){
    return new Promise((resolve,reject)=>{
      wx.uploadFile({
        filePath: filePath,
        name: 'file',
        url: 'http://47.111.12.59:8080/uploadExcel',
        success:(d)=>resolve(d),
        file:(e)=>reject(e)
      })
    })
  },

  request:function({url,data,method='POST'} = obj){
    return new Promise((resolve,reject)=>{
      wx.request({
        url: url,
        data:data,
        method:method,
        success:resolve(d),
        file:reject(e)
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})